package MONITORING::DB::MYSQL::TABLE_CONSISTENCE;

#################################################################
# $LastChangedDate: 2013-11-25 15:03:06 +0000 (Mon, 25 Nov 2013) $
# $LastChangedRevision: 22 $
# $LastChangedBy: aswod_83@gmx.net $
#################################################################

use strict;
use base qw(moniCA::DBH::MySQL);
use XML::Simple;
use Data::Dumper;

#-------------------------------------------------------------------------------
sub Run {

#-------------------------------------------------------------------------------
# Description:
# Input Parms:
# Output Parms: None
#-------------------------------------------------------------------------------

    my $self   = shift;
    my $cnf    = $self->GetCNF();
    my $logger = $self->GetLogger();

    foreach ( $self->_getTables() ) {
        my @output = $self->_doQuery_check_table($_);
        if ( $output[2] =~ /error/i ) {
            $logger->error( $output[1] . ' ' . $output[0] . ': ' . $output[3] );
        }
        elsif ( $output[2] =~ /warning/i ) {
            $logger->warn( $output[1] . ' ' . $output[0] . ': ' . $output[3] );
        }
        elsif ( $output[2] =~ /info/i ) {
            $logger->info( $output[1] . ' ' . $output[0] . ': ' . $output[3] );
        }
        elsif ( $output[2] =~ /status/i ) {
            $logger->info( $output[1] . ' ' . $output[0] . ': ' . $output[3] );
        }
    }
}

#-------------------------------------------------------------------------------
sub _getTables {

#-------------------------------------------------------------------------------
# Description:
# Input Parms:
# Output Parms:
#-------------------------------------------------------------------------------

    my $self = shift;
    $self->ConnectDB();
    my $stmt = q{ show tables };
    my $sth  = $self->{DBH}->prepare($stmt);
    $sth->execute();
    my @tables = map { $_->[0] } @{ $sth->fetchall_arrayref };
    $sth->finish();
    $self->DisconnectDB();
    return @tables;
}

#-------------------------------------------------------------------------------
sub _doQuery_check_table {

#-------------------------------------------------------------------------------
# Description:
# Input Parms:
# Output Parms:
#-------------------------------------------------------------------------------

    my $self = shift;
    $self->ConnectDB();
    my $table = shift;
    my $stmt  = qq{ check table $table };
    my $sth   = $self->{DBH}->prepare($stmt);
    $sth->execute();
    my @output = $sth->fetchrow_array();
    $sth->finish();
    $self->DisconnectDB();
    return @output;
}

1;
__END__
 
=pod
 
=head1 NAME
 
TABLESPACE_USAGE.pm - monitors tablespace usage 
 
=head1 SYNOPSIS
 
moniCA.pl -pcheck_oracle_tablespace_usage
 
        Options:
                --help  help message for check_processes
                --conf  configfile add/merge configuration
                -v              verbose DEBUG
                -vv             verbose INFO
                --param var1=value --param var2=value ...
                                parameter var1, var2 ... add/merge configuration
 
 
=head1 DESCRIPTION
 
=head1 PREREQUISITES
 
The follwing CPAN modules needs to be installed:
 
C<Log::Log4perl>
C<XML::Simple>
 
=head1 CONFIGURATION FILE
 
  By default
 
=head2 EXAMPLES
 
=head3 example of configuation file
 
Modul 	    MONITORING::DB::ORACLE::TABLESPACE_USAGE
Logger		%LOGGER%
LogLevel	%PRIORITY%



<DBConnect>
	ConnectString	dbi:Oracle:SID=%INSTANCE%;HOST=%IP%;port=1521
	dbuser		%USER%
	dbpw		%PASSWORD%
	description 	Oracle Database
</DBConnect>


 
=cut
